
/*function setUser(userId,userName) {
	User.id = userId;
	User.name = userName;
}*/

function changeColor(element, color) {
		//element = document.getElementById(id);
		//event.cancelBubble = true;
		//oldColor = element.currentStyle.background;
		element.style.background = color;
}

function show(element,bool) {
	
	if(bool)
		element.style.visibility = 'visible';
	else
		element.style.visibility = 'hidden';
}

function showById(id,bool) {
	show(document.getElementById(id),bool);
}

function showLoginForm() {
	
	var id ='#loginForm1';
	var loginForm = document.getElementById('loginForm1');
	
	var winH = $(window).height();
	var winW = $(window).width();
	
	$(id).css('top',  winH/2-$(id).height()/2);
	$(id).css('left', winW/2-$(id).width()/2);
	
	if(loginForm.style.visibility == 'visible') {
		show(loginForm, false);		
	} else if(loginForm.style.visibility == 'hidden') {
		show(loginForm, true);
		document.getElementById('loginText').focus();
	}
}

function showUserMenu() {
	
	var userMenu = document.getElementById('userMenu');
	if(userMenu.style.visibility == 'visible') {
		show(userMenu, false);
		
	} else if(userMenu.style.visibility == 'hidden') {
		show(userMenu, true);
	}
}

function login() {
	document.forms["loginForm"].submit();
}

function logout() {
	document.forms["logoutForm"].submit();
}

function getCookie(name) {
	
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}

function get_cookies_array() {
	
    var cookies = { };
    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {
            var name_value = split[i].split("=");
            name_value[0] = name_value[0].replace(/^ /, '');
            cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
        }
    }
    
    for(var name in cookies) {
    	alert( name + " : " + cookies[name]);
    }    
    return cookies;
}

function sendMessage() {
	alert('sendMessage');
	document.getElementById('VideoApp').sendMessage('testName','testMessage');
}

function chatSendMessage() {
	var message = document.getElementById('chatText').value;
	document.getElementById('chatText').value = "";
	document.getElementById('VideoApp').sendMessage(message);
	return false;
}

function chatPublishMessage(user,message) {
	
	var newDiv = document.createElement("DIV");
	newDiv.className = "chatMessage";
	//newDiv.style.textAlign = "left";
	//newDiv.style.paddingLeft = "5px";
	//newDiv.style.width = "200px";
    newDiv.innerHTML = "<b>"+user+"</b>: "+message;
    document.getElementById("chatArea").appendChild(newDiv);
	
	//document.getElementById('chatArea').value += "&#10;"+message;
}

///////////////////////////

var titleCode = '';
var inout = 'out';

function showchat() {
	
	var chat = document.getElementById('chatDiv');
	if(chat.style.visibility == 'visible') {
		showById('chatDiv',false);
	} else if(chat.style.visibility == 'hidden') {
		showById('chatDiv',true);
	} else {
		alert("else");
	}
}

function openStream(id) {
	
	var sessionId = getCookie('JSESSIONID');
	var vW;
	var vH;
	var vF;
	var vQ;
	if(inout == 'out') {
		vW = document.getElementById('vwidth').value;
		vH = document.getElementById('vheight').value;
		vF = document.getElementById('vfps').value;
		vQ = document.getElementById('vquality').value;
	} else {
		vW = '320';
		vH = '240';
		vF = '20';
		vQ = '100';
	}
	var embedCode = '<embed src="flash/VideoApp.swf" width="650" height="500" align="middle" id="VideoApp" quality="high" bgcolor="#ffffff" ' +
	'name="VideoApp" allowscriptaccess="sameDomain" flashvars="myName=streamId=' + id + '&sid=' + sessionId + '" ' +
	'pluginspage="http://www.adobe.com/go/getflashplayer"  type="application/x-shockwave-flash"></embed>';
	
	var textAreaCode = '<textarea id="chatArea" cols="20" rows="20" readonly="yes"></textarea>';
	
	var chatCode = '<div style="color:#999;"><input type="button" class="chatButton" value="Chat" onclick="showchat()"/></div><div id="chatDiv" class="chat" style="visibility:hidden;">' +
	'<div id="chatArea" style="width:200px; height:430px; align-text:left; background:white; overflow-y:auto; border:1px solid #CCC; padding-top:5px;"></div>' +
	'<form onsubmit="return chatSendMessage()"><div style="float:left; width:100px;"><input id="chatText" type="text" size="18"></div>' +
	'<div style="float:right;"><input type="submit" value="send"></div></form></div>';
	
	var objectCode = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
	'width="640" height="480"' +
	'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">' +
	'<param name="movie" value="flash/VideoApp.swf" />' +
	'<param name="quality" value="high" />' +
	'<param name="bgcolor" value="#ffffff" />' +
	'<param name="allowScriptAccess" value="sameDomain" />' +
	'<param name="FlashVars" value="streamId=' + id + '&sid=' + sessionId + '&vw='+vW+'&vh='+vH+'&vf='+vF+'&vq='+vQ+'" />' + 
	'<embed src="flash/VideoApp.swf" quality="high" bgcolor="#ffffff"' +
		'width="640" height="480" name="VideoApp" id="VideoApp" align="middle"' +
		'play="true"' +
		'loop="false"' +
		'quality="high"' +
		'allowScriptAccess="sameDomain"' +
		'type="application/x-shockwave-flash" ' +
		'FlashVars="streamId=' + id + '&sid=' + sessionId + '&vw='+vW+'&vh='+vH+'&vf='+vF+'&vq='+vQ+'" ' +
		'pluginspage="http://www.adobe.com/go/getflashplayer">' +
	'</embed>' +
'</object>';
	
	var code = '<div id="boxes">' +
			   '<div id="dialog" class="window">' + 
			   '<div class="videoHeader">' + titleCode + '</div>' +
			   '<div style="float:right;"><a href="#" class="close"><img src="images/cancel.gif" width="20"/></a></div>' +
			   '<div id="videoContent">' +
			   '<div class="videoFrame">' +
			   objectCode +
			   '</div>' + 
			   '<div class="chatFrame">'+chatCode+'</div>' +
			   '</div></div>' +
			   '<div id="mask"></div>' +
			   '</div>';
	
	document.getElementById('modalWin').innerHTML = code;	
	showModalWindow();	
}

function openInStream(id,title,masterId,masterName) {
	
	alert("openInStream id: " + id);
	//alert("openInStream title: " + title + ", master: " + masterName);
	//var code = '<div>Title: '+title+'</div><div>Author: '+masterName+'</div>';
	//document.getElementById('modalWin').innerHTML = code;
	inout = 'in';
	titleCode = title+' - '+ '<a href="streams.jsp?authId='+masterId+'" target="_blank">'+masterName+'</a>';
	openStream(id);
}

function createStream() {

	var name = document.getElementById('streamName').value;
	var category = document.getElementById('streamCategory').value;
	var imageName = document.getElementById('imageName').value;
	
	var request = false;
	if(navigator.appName == "Microsoft Internet Explorer") {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		request = new XMLHttpRequest();
	}
	
	//
	var params = 'name=' + encodeURIComponent(name) + '&category=' + category + '&image=' + imageName;
	request.open('POST', 'AddStream');
	//request.open('GET', 'AddStream?' + params);
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	//request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//request.setRequestHeader("Content-length", params.length);
	//request.setRequestHeader("Connection", "close");
	
	request.onreadystatechange = function() {
		
		if(request.readyState == 4) {			
			if(request.status == 200) {
									
				var data = eval("("+request.responseText+")");
				var id = data.id;			
				openStream(id);
				
			} else {				
				alert("You need to sign in to start a speech.");
			}
		}
	};
	
	request.send(params);	
}
	
function showModalWindow() {

	//alert('showModalWindow');
	//select all the a tag with name equal to modal
	
	//$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		//e.preventDefault();
		//Get the A tag
		//var id = $(this).attr('href');
		var id ='#dialog';
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		//$('#mask').fadeIn(10);	
		$('#mask').fadeTo("fast",0.5);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(10); 
		
		//if close button is clicked
	$('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });     
     
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });          
}

function getClientWidth() {
	
	var myWidth = 0;
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
	} else if(document.documentElement && document.documentElement.clientWidth) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	} else if(document.body && document.body.clientWidth) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	}
	return myWidth;
}

function getClientHeight() {
	
	var myHeight = 0;
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
	    myHeight = window.innerHeight;
	} else if(document.documentElement && document.documentElement.clientHeight) {
	    //IE 6+ in 'standards compliant mode'
	    myHeight = document.documentElement.clientHeight;
	} else if(document.body && document.body.clientHeight) {
	    //IE 4 compatible
	    myHeight = document.body.clientHeight;
	}
	return myHeight;
}

function alertSize() {
	  
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number') {
	//Non-IE
		myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	}
	  window.alert( 'Width = ' + myWidth );
	  window.alert( 'Height = ' + myHeight );
}

function filterStreams(categoryId) { 	
	document.getElementById('SphereApp').update(categoryId);
}

function searchStreams() {
	var searchTemplate = document.getElementById('searchInput').value;
	document.getElementById('SphereApp').search(searchTemplate);
	return false;
}

function closeStream(streamId,streamName) {
	//alert('closeStream streamId: '+streamId+', streamName: '+streamName);
	
	
	var code =  '<div style="margin-top:25px; font-size:16px; font-weight:bold; color:#444;">Stream is finished</div>' + 
				'<div style="color:#444;">You can leave your comments <a href="stream.jsp?streamId='+streamId+'">here</a></div>';
	   			
		
		
	document.getElementById('videoContent').innerHTML = code;	
	//$('#mask, .window').hide();
}

